/** Powered By zscat科技, Since 2016 - 2020 */

package com.zs.pig.order.serviceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.zs.pig.common.utils.RandomString;
import com.zs.pig.goods.api.model.Cart;
import com.zs.pig.goods.api.model.GoodsOrder;
import com.zs.pig.goods.api.service.CartService;
import com.zs.pig.goods.api.service.GoodsOrderService;
import com.zs.pig.order.api.model.Order;
import com.zs.pig.order.api.model.OrderLog;
import com.zs.pig.order.api.service.OrderService;
import com.zs.pig.order.mapper.OrderLogMapper;
import com.zs.pig.order.mapper.OrderMapper;
/**
 * 
 * @author zsCat 2017-1-7 16:06:55
 * @Email: 951449465@qq.com
 * @version 1.0v
 *	订单管理
 */
@Service("OrderService")
public class OrderServiceImpl  implements OrderService {

	@Resource
	private OrderMapper OrderMapper;
	
	@Resource
	private GoodsOrderService GoodsOrderServer;
	@Resource
	private  CartService CartService;
	@Resource
	private OrderLogMapper  OrderLogMapper;
	
	/**
	 * 保存或更新
	 * 
	 * @param Order
	 * @return
	 */
	public int saveOrder(Order Order) {
		return OrderMapper.insertSelective(Order);
	}

	/**
	 * 删除
	* @param Order
	* @return
	 */
	public int deleteOrder(Order Order) {
		return OrderMapper.deleteByPrimaryKey(Order.getId());
	}

   
	@Override
	public Order insertOrder(String[] cartIds,Long addressid, Long paymentid, String usercontent,Long uid,String uname) {
		Order order=new Order();
		if(cartIds!=null && cartIds.length>0){
			int count=0;
			BigDecimal total=BigDecimal.ZERO;
			for(String cartId:cartIds){
				Cart cart=CartService.selectByPrimaryKey(Long.parseLong(cartId));
				if(cart==null){
					return null;
				}
				count+=cart.getCount();
				 total =total.add(BigDecimal.valueOf(Double.valueOf(cart.getPrice())).multiply(BigDecimal.valueOf(cart.getCount())));
				GoodsOrder go=new GoodsOrder();
				go.setGoodsid(cart.getGoodsid());
				go.setOrderid(order.getId());
				GoodsOrderServer.insertSelective(go);
				CartService.deleteByPrimaryKey(cart);
			}
			order.setOrdersn(RandomString.generateRandomString(8));
			order.setCreatedate(new Date());
			order.setStatus(1);
			order.setUserid(uid);
			order.setUsername(uname);
		//	order.setPaymentid(paymentid);
			order.setUsercontent(usercontent);
			order.setAddressid(addressid);
			//order.setOrderTotalPrice();
			OrderMapper.insertSelective(order);
			
			OrderLog log=new OrderLog();
			log.setOrderId(order.getId());
			log.setOrderState("1");
			log.setStateInfo("提交订单");
			log.setCreateTime(new Date().getTime());
			log.setOperator(uname);
		//	OrderLogMapper.insertSelective(log);
			
			order.setTotalcount(count);
			order.setTotalprice(total);
			OrderMapper.updateByPrimaryKey(order);
		}
		return order;
		
	}

	@Override
	public PageInfo<Order> selectPage(int pageNum, int pageSize, Order order) {
		return null;
	}

	@Override
	public Order selectByPrimaryKey(Long id) {
		return OrderMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Order> select(Order o) {
		return OrderMapper.select(o);
	}

	@Override
	public void update(Order o) {
		OrderMapper.updateByPrimaryKey(o);
	}

}
